<?php

/**
 * EmsImageFile form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Jacobo Martínez
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsImageFileForm extends BaseEmsImageFileForm
{
    public function configure()
    {
        unset($this['image_id'], $this['size_id']);

        $this->setWidget('file', new sfWidgetFormInputHidden());

        $this->setValidator('file',new sfValidatorString(array('required' => false)));

        $this->setWidget('file_type', new sfWidgetFormInputHidden());

        $this->setValidator('file_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('file_size', new sfWidgetFormInputHidden());

        $this->setValidator('file_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('tmp_file', new sfWidgetFormInputFile());

        $this->setValidator('tmp_file',new sfValidatorString(array('required' => false)));

        $this->setWidget('preview', new sfWidgetFormInputHidden());

        $this->setValidator('preview',new sfValidatorString(array('required' => false)));

        $this->setWidget('preview_size', new sfWidgetFormInputHidden());

        $this->setValidator('preview_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('preview_type', new sfWidgetFormInputHidden());

        $this->setValidator('preview_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->widgetSchema->setLabels(array('tmp_file' => $this->getObject()->getSize()));

        if($this->getObject()->exists() && $this->getObject()->getFile()){
            $file = $this->getObject()->getFile();

            $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id').'/images/';

            if(is_file($folder.$file)){
                $file_size = filesize($folder.$file);

                $this->setDefaults(array('file_size' => $file_size));
            } else {
                $this->setDefaults(array('file_size' => 0));
            }
        }
    }

    protected function doUpdateObject($values)
    {
        if(strcasecmp($values['file'],'') != 0){
            $path_parts_file = pathinfo($values['file']);

            $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id');

            if($this->getObject()->exists()){
               if(strcasecmp($path_parts_file['basename'], $this->getObject()->getFile()) != 0){
                   $validated_file = new sfValidatedFile(
                                            $path_parts_file['basename'],
                                            $values['file_type'],
                                            $values['file'],
                                            $values['file_size'],
                                            $folder.'/images/'
                                        );

                   $values['file'] = $validated_file->save();

                   $validated_preview = new sfValidatedFile(
                                            $values['preview'],
                                            $values['preview_type'],
                                            $folder.'/temp_files/'.$values['preview'],
                                            $values['preview_size'],
                                            $folder.'/images/'
                                        );

                   $values['preview'] = $validated_preview->save();
               }
            } else {
                $validated_file = new sfValidatedFile(
                                            $path_parts_file['basename'],
                                            $values['file_type'],
                                            $values['file'],
                                            $values['file_size'],
                                            $folder.'/images/'
                                        );

                $values['file'] = $validated_file->save();

                $validated_preview = new sfValidatedFile(
                                            $values['preview'],
                                            $values['preview_type'],
                                            $folder.'/temp_files/'.$values['preview'],
                                            $values['preview_size'],
                                            $folder.'/images/'
                                        );

               $values['preview'] = $validated_preview->save();
            }
        }

        unset($values['file_type'], $values['file_size'], $values['tmp_file'],
                $values['preview_type'], $values['preview_size']
            );

        parent::doUpdateObject($values);
    }
}
